package revenge.jack_cristo.shiyuanbaoyou;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import revenge.jack_cristo.shiyuanbaoyou.adapter.Adapter_Shouye;
import revenge.jack_cristo.shiyuanbaoyou.bean.Shouye_listbean;
import revenge.jack_cristo.shiyuanbaoyou.customview.MyGridview;
import revenge.jack_cristo.shiyuanbaoyou.main_interface.Shiyuangou_interface;

/**
 * Created by Jack-Cristo on 2016/11/13.
 */

public class Shiyuangou_fragment extends Fragment {
    private static Context context;
    private TabLayout tabLayout_zhide;
    private MyGridview gridView;
    private String baseurl = "http://www.syby8.com/apptools/";
    private Map<String, String> map = new HashMap<>();
    private List<Shouye_listbean.RowsBean> list_product = new ArrayList<>();
    private Adapter_Shouye adapter;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    gridView.setAdapter(adapter);
                    break;

            }
        }
    };

    public static Shiyuangou_fragment newInstance(Context con) {
        context = con;
        Bundle args = new Bundle();

        Shiyuangou_fragment fragment = new Shiyuangou_fragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.shiyuangou_feame, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        inittab();
        map.clear();
        //act=&v=34&pages=1&bc=0&sc=0&sorts=&channel=9&ckey=&daynews=&lprice=0&hprice=0
        // &tbclass=0&actid=0&brandid=0&predate=&index=0

        map.put("act", "getproductlist");
        map.put("pages", "1");
        map.put("bc", "0");
        map.put("sc", "0");
        map.put("sorts", "");
        map.put("channel", "9");
        map.put("ckey", "");
        map.put("daynews", "");
        map.put("lprice", "0");
        map.put("hprice", "0");
        map.put("tbclass", "0");
        map.put("daynews", "");
        map.put("actid", "0");
        map.put("brandid", "0");
        map.put("predate", "");
        map.put("index", "0");

        initgridviewdata();
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                Intent intent = new Intent();
                intent.putExtra("weburl", list_product.get(i).getProductUrl());
                intent.setClass(context, WebviewActivity.class);
                startActivity(intent);


            }
        });
    }

    private void initgridviewdata() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseurl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Shiyuangou_interface shouye_interface = retrofit.create(Shiyuangou_interface.class);
        Call<Shouye_listbean> call = shouye_interface.getshiyuangoulist(map);
        call.enqueue(new Callback<Shouye_listbean>() {
            @Override
            public void onResponse(Call<Shouye_listbean> call, Response<Shouye_listbean> response) {
                if (response.isSuccessful()) {
                    list_product = response.body().getRows();
                    adapter = new Adapter_Shouye(context, list_product);
                }
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onFailure(Call<Shouye_listbean> call, Throwable t) {

            }
        });
    }

    private void inittab() {
        TabLayout.Tab tab1 = tabLayout_zhide.newTab().setText("精选");
        tabLayout_zhide.addTab(tab1);
        TabLayout.Tab tab2 = tabLayout_zhide.newTab().setText("10元");
        tabLayout_zhide.addTab(tab2);
        TabLayout.Tab tab3 = tabLayout_zhide.newTab().setText("20元");
        tabLayout_zhide.addTab(tab3);
        TabLayout.Tab tab4 = tabLayout_zhide.newTab().setText("30元");
        tabLayout_zhide.addTab(tab4);

        tabLayout_zhide.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
                        map.clear();
                        map.put("act", "getproductlist");
                        map.put("pages", "1");
                        map.put("bc", "0");
                        map.put("sc", "0");
                        map.put("sorts", "");
                        map.put("channel", "9");
                        map.put("ckey", "");
                        map.put("daynews", "");
                        map.put("lprice", "0");
                        map.put("hprice", "0");
                        map.put("tbclass", "0");
                        map.put("daynews", "");
                        map.put("actid", "0");
                        map.put("brandid", "0");
                        map.put("predate", "");
                        map.put("index", "0");
                        break;
                    case 1:
                        map.clear();
                        map.put("act", "getproductlist");
                        map.put("pages", "1");
                        map.put("bc", "0");
                        map.put("sc", "0");
                        map.put("sorts", "");
                        map.put("channel", "3");
                        map.put("ckey", "");
                        map.put("daynews", "");
                        map.put("lprice", "0");
                        map.put("hprice", "0");
                        map.put("tbclass", "0");
                        map.put("daynews", "");
                        map.put("actid", "0");
                        map.put("brandid", "0");
                        map.put("predate", "");
                        map.put("index", "0");
                        break;
                    case 2:
                        map.clear();
                        map.put("act", "getproductlist");
                        map.put("pages", "1");
                        map.put("bc", "0");
                        map.put("sc", "0");
                        map.put("sorts", "");
                        map.put("channel", "4");
                        map.put("ckey", "");
                        map.put("daynews", "");
                        map.put("lprice", "0");
                        map.put("hprice", "0");
                        map.put("tbclass", "0");
                        map.put("daynews", "");
                        map.put("actid", "0");
                        map.put("brandid", "0");
                        map.put("predate", "");
                        map.put("index", "0");
                        break;
                    case 3:
                        map.clear();
                        map.put("act", "getproductlist");
                        map.put("pages", "1");
                        map.put("bc", "0");
                        map.put("sc", "0");
                        map.put("sorts", "");
                        map.put("channel", "5");
                        map.put("ckey", "");
                        map.put("daynews", "");
                        map.put("lprice", "0");
                        map.put("hprice", "0");
                        map.put("tbclass", "0");
                        map.put("daynews", "");
                        map.put("actid", "0");
                        map.put("brandid", "0");
                        map.put("predate", "");
                        map.put("index", "0");
                        break;
                }
                initgridviewdata();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tabLayout_zhide = (TabLayout) view.findViewById(R.id.tab_zhide);
        gridView = (MyGridview) view.findViewById(R.id.gride_zhide);
    }
}
